/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package andreafrancia.util.remote;

import java.rmi.RemoteException;
import javax.swing.event.EventListenerList;

/**
 *
 * @author Andrea Francia andrea.francia at gmx dot org
 */
public class RemoteExceptionListenersSupport {
    private EventListenerList eventListenerList = new EventListenerList();

    /**
     * Add a {@link RemoteExceptionListener}.
     * @param listener the listener to add.
     */
    public void addRemoteExceptionListener(RemoteExceptionListener listener) {
        eventListenerList.add(RemoteExceptionListener.class, listener);
    }

    /**
     * Remove a {@link RemoteExceptionListener}.
     * This method performs 
     * no function, nor does it throw an exception, if the listener 
     * specified by the argument was not previously added to this component.
     * If listener <code>l</code> is <code>null</code>,
     * no exception is thrown and no action is performed.     
     * @param listener the listener to add.
     */
    public void removeRemoteExceptionListener(RemoteExceptionListener listener) 
    {
        eventListenerList.remove(RemoteExceptionListener.class, listener);
    }

    /**
     * Returns an array of all the <code>RemoteExceptionListener</code>s added
     * with addRemoteExceptionListener().
     *
     * @return all of the <code>RemoteExceptionListener</code>s added or 
     *         an empty array if no listeners have been added
     */
    public RemoteExceptionListener[] getRemoteExceptionListeners() {
        return eventListenerList.getListeners(RemoteExceptionListener.class);
    }

    public void fireExceptionThrown(RemoteException ex) {
        RemoteExceptionListener[] listeners;
        listeners = eventListenerList.getListeners(RemoteExceptionListener.class);
        for (RemoteExceptionListener listener : listeners) {
            listener.exceptionThrown(ex);
        }
    }
}
